package cn.wolfcode.wms.web.controller;


import cn.wolfcode.wms.domain.Employee;
import cn.wolfcode.wms.domain.Role;
import cn.wolfcode.wms.mapper.DepartmentMapper;
import cn.wolfcode.wms.query.EmployeeQueryObject;
import cn.wolfcode.wms.query.QueryObject;
import cn.wolfcode.wms.service.IDepartmentService;
import cn.wolfcode.wms.service.IEmployeeService;
import cn.wolfcode.wms.service.IRoleService;
import cn.wolfcode.wms.util.JSONResult;
import cn.wolfcode.wms.util.NeedPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired//用于查询所有部门
    private IDepartmentService departmentService;
    @Autowired//因为在编辑界面中需要回显所有角色
    private IRoleService roleService;

    @NeedPermission("员工列表")
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") EmployeeQueryObject qo, Model model){


        model.addAttribute("result",employeeService.queryResult(qo));
        model.addAttribute("depts",departmentService.selectList());
        return "employee/list";
    }

    @NeedPermission("员工编辑")
    @RequestMapping("input")
    public String input(Model model,Long id){

        if (id != null) {
            Employee employee = employeeService.get(id);
            model.addAttribute("entity",employeeService.get(id));
        }
        //查询所有的部门
        model.addAttribute("depts",departmentService.selectList());
        //查询所有的角色
        model.addAttribute("roles",roleService.selectList());
        return "employee/input";
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id){

        employeeService.delete(id);

        return new JSONResult();
    }
    @RequestMapping("deletes")
    @ResponseBody
    public Object deletes(Long[] ids){

        employeeService.deletes(ids);

        return new JSONResult();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Employee d,Long[] roleIds){

        employeeService.saveOrUpdate(d,roleIds);

        return new JSONResult();
    }
}
